package com.mail.exception;

import com.mail.entity.ErrorInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 刘浩然
 * @Date: 2021/5/8 11:54
 */
@ControllerAdvice
public class mailSendException {

    @ExceptionHandler(value = MyException.class)
//    @ResponseBody
    public ModelAndView jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
        ErrorInfo<String> r = new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(ErrorInfo.ERROR);
        r.setData("发送邮件异常");
        r.setUrl(req.getRequestURL().toString());

        ModelAndView mav = new ModelAndView();
        mav.addObject("ex", r);
        mav.addObject("url", req.getRequestURL());
        mav.setViewName("error");
        return mav;
    }
}
